package gudaziEx;

import alarm.ZLable;
import alarm.ZTextField;
import alarm.imp.ZtextDo;
import alarm.imp.ZtextDoForRunningChainCorn;
import com.melloware.jintellitype.JIntellitype;
import commons.entity.CommonModules;
import commons.entity.Constant;
import commons.entity.NativeCp;
import commons.util.GameUtil;
import commons.util.MySpringUtil;
import fgoScript.entity.BaseZButton;
import fgoScript.entity.Zpanel;
import fgoScript.entity.panel.inte.PanelModel;
import gudaziEx.entity.MyComboBoxUI;
import gudaziEx.entity.MyListCellRenderer;
import gudaziEx.entity.RunningChain;
import gudaziEx.service.ClickManage;
import gudaziEx.util.ChainUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.swing.*;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ClickEXPanel extends Zpanel implements PanelModel {
    private static final Logger LOGGER = LogManager.getLogger(ClickEXPanel.class);

    private CommonModules commonModules;
    public ClickEXPanel(CommonModules commonModules) {
        this.commonModules = commonModules;
        initP();
    }
    private ClickManage clickManage;
    @Override
    public void initP() {
        int len = 2;
        Zpanel linePanel = new Zpanel();
        Zpanel ifLoginPanel = new Zpanel();
        Zpanel scriptPanel = new Zpanel();
        Zpanel runnablePanel = new Zpanel();
        Zpanel cornPanel = new Zpanel();
        Zpanel toolsPanel = new Zpanel();
        Zpanel autoScroll = new Zpanel();
        linePanel.setLayout(new GridLayout(1, 3));
        linePanel.setBounds(5, 5 + (13 * (30+len)), 320, 25+len);
        linePanel.setOpaque(false);
        ifLoginPanel.setLayout(new GridLayout(1, 2));
        scriptPanel.setLayout(new GridLayout(1, 3));
        scriptPanel.setBounds(5, 5 + (11 * (30+len)), 320, 25+len);
        scriptPanel.setOpaque(false);
        runnablePanel.setLayout(new GridLayout(1, 3));
        runnablePanel.setBounds(5, 5 + (9 * (30+len)), 320, 25+len);
        runnablePanel.setOpaque(false);
        cornPanel.setLayout(new GridLayout(1, 3));
        cornPanel.setBounds(5, 5 + (10 * (30+len)), 320, 25+len);
        cornPanel.setOpaque(false);
        toolsPanel.setLayout(new GridLayout(1, 3));
        toolsPanel.setBounds(5, 5 + (8 * (30+len)), 320, 25+len);
        toolsPanel.setOpaque(false);
        autoScroll.setLayout(new GridLayout(1, 3));
        autoScroll.setBounds(5, 5 + (7 * (30+len)), 320, 25+len);
        autoScroll.setOpaque(false);
        //注册 检测快捷键按钮
        BaseZButton checkPointButton = new BaseZButton(null, 0,"Alt+1",JIntellitype.MOD_ALT, KeyEvent.VK_1,false, false, BaseZButton.pink) {
            private static final long serialVersionUID = 6504858991507730448L;
            @Override
            public void runMethod() {
                ClickManage.setChecker();
            }
        };
        //注册 点击快捷键按钮
        BaseZButton ClickPointButton = new BaseZButton(null, 0,"Alt+2",JIntellitype.MOD_ALT, KeyEvent.VK_2,false, false, BaseZButton.pink) {
            private static final long serialVersionUID = 6504858991507730448L;
            @Override
            public void runMethod() {
                ClickManage.setClicker();
            }
        };
        //注册 删除临时json
        BaseZButton deleteTempJsonButton = new BaseZButton(null, 0,"清空",JIntellitype.MOD_SHIFT, (int) 'P',true, true, BaseZButton.pink) {
            private static final long serialVersionUID = 6504858991507730448L;
            @Override
            public void runMethod() {
                ChainUtil.deleteTempFile();
            }
        };
        //注册 点击快捷键按钮
        BaseZButton StartTempClickButton = new BaseZButton(null, 0,"临时按键(Alt+X)",JIntellitype.MOD_ALT, KeyEvent.VK_1, false, false, BaseZButton.pink) {
            private static final long serialVersionUID = -1900866258318497377L;
            @Override
            public void runMethod() {
                clickManage.tempToggle();
            }
        };
        //注册 自动滚屏按钮
        BaseZButton autoScrollbtn = new BaseZButton(null, 0,"自动滑屏(Alt+1)",JIntellitype.MOD_ALT, KeyEvent.VK_1, false, false, BaseZButton.pink) {
            private static final long serialVersionUID = -1900866258318497377L;
            @Override
            public void runMethod() {
                clickManage.autoScroll();
            }
        };
        //注册 自动滚屏按钮
        BaseZButton autoWhellUpbtn = new BaseZButton(null, 0,"向上滚轮(Alt+2)",JIntellitype.MOD_ALT, KeyEvent.VK_2, false, false, BaseZButton.pink) {
            private static final long serialVersionUID = -1900866258318497377L;
            @Override
            public void runMethod() {
                clickManage.autoWhell(1);
            }
        };
        //注册 自动滚屏按钮
        BaseZButton autoWhellDownbtn = new BaseZButton(null, 0,"向下滚轮(Alt+3)",JIntellitype.MOD_ALT, KeyEvent.VK_3, false, false, BaseZButton.pink) {
            private static final long serialVersionUID = -1900866258318497377L;
            @Override
            public void runMethod() {
                clickManage.autoWhell(-1);
            }
        };

        //按键倍率按钮
        ZTextField zf2 = new ZTextField(new ZtextDo("0", "changeButton_" + NativeCp.getUserName(),"", "multiFactorForScroll"));
        zf2.setBounds(275, 5 + (12 * (30+len)), 50, 25+len);
        //按键倍率按钮
        ZTextField zf3 = new ZTextField(new ZtextDo("0", "changeButton_" + NativeCp.getUserName(),"", "scrollStep"));
        zf3.setBounds(275, 5 + (12 * (30+len)), 50, 25+len);

        //按键倍率按钮
        ZTextField zf4 = new ZTextField(new ZtextDo("0", "changeButton_" + NativeCp.getUserName(),"", "multiFactorForWhell"));
//        zf2.setBounds(275, 5 + (12 * (30+len)), 50, 25+len);
        //按键倍率按钮
        ZTextField zf5 = new ZTextField(new ZtextDo("0", "changeButton_" + NativeCp.getUserName(),"", "whellStep"));
//        zf3.setBounds(275, 5 + (12 * (30+len)), 50, 25+len);
        //按键倍率按钮
        ZTextField zf6 = new ZTextField(new ZtextDo("0", "changeButton_" + NativeCp.getUserName(),"", "restTimeForWhell"));
//        zf2.setBounds(275, 5 + (12 * (30+len)), 50, 25+len);
        //按键倍率按钮
        ZTextField zf7 = new ZTextField(new ZtextDo("0", "changeButton_" + NativeCp.getUserName(),"", "restStepForWhell"));
//        zf3.setBounds(275, 5 + (12 * (30+len)), 50, 25+len);
        //脚本相对路径名称
        JComboBox jb = getScriptComboBox();
        JComboBox rnjb = getRunnableComboBox();
        //注册 是否登录按钮
        BaseZButton ifLoginButton = new BaseZButton("",BaseZButton.pink) {
            private static final long serialVersionUID = 638308570629886471L;
            @Override
            public void runMethod() {
                this.activeButtonForRunningChain();
            }
        };
        //注册 点击快捷键按钮
        BaseZButton StartClickButton = new BaseZButton(null, 0,"挂机按键(Alt+Q)",JIntellitype.MOD_ALT, (int) 'Q', false, false, BaseZButton.pink) {
            private static final long serialVersionUID = -1900866258318497377L;
            @Override
            public void runMethod() {
                setLoginByView(ifLoginButton);
                clickManage.Toggle(jb.getSelectedItem().toString(),false);
            }
        };
        // corn表达式 text
        ZTextField cornZt  = getCornTextField();
        //注册 点击快捷键按钮
        BaseZButton runnableButton = new BaseZButton(null, 0,"定时任务(Alt+E)",JIntellitype.MOD_ALT, (int) 'E', false, false, BaseZButton.pink) {
            private static final long serialVersionUID = -1900866258318497377L;
            @Override
            public void runMethod() {
                clickManage.doCornRunnable(cornZt.getText(), rnjb.getSelectedItem().toString());
            }
        };
        //睡眠按钮
        List<String> changeList = new ArrayList<>();
        changeList.add("清醒");
        changeList.add("睡眠");
        BaseZButton sleepBt = BaseZButton.getChangeListButton(changeList,"changeButton_" + NativeCp.getUserName(),"", "sleepMode", true, true, BaseZButton.pink);
        //注册 点击快捷键按钮
        BaseZButton pauseBtn = commonModules.getPauseBtn();

        ifLoginButton.setExcuteble(true);
        RunningChain rc = ChainUtil.getRunningChain();
        ifLoginButton.setText(rc.isLogin()==true ? "√" : " ");
        ZLable ifLoginLable = new ZLable(" 登录");
        StartTempClickButton.setBounds(5, 5 + (12 * (30+len)), 120, 25+len);
        StartClickButton.setBounds(125, 5 + (12 * (30+len)), 120, 25+len);
        ifLoginLable.setBounds(245, 5 + (12 * (30+len)), 40, 25+len);
        ifLoginButton.setBounds(285, 5 + (12 * (30+len)), 40, 25+len);

        autoWhellUpbtn.setBounds(5, 5 + (6 * (30+len)), 80, 25+len);
        autoWhellDownbtn.setBounds(85, 5 + (6 * (30+len)), 80, 25+len);
        zf4.setBounds(165, 5 + (6 * (30+len)), 30, 25+len);
        zf5.setBounds(195, 5 + (6 * (30+len)), 30, 25+len);
        zf6.setBounds(225, 5 + (6 * (30+len)), 50, 25+len);
        zf7.setBounds(275, 5 + (6 * (30+len)), 50, 25+len);


        StartTempClickButton.setAllowRepeat(true);
        StartClickButton.setAllowRepeat(true);
        linePanel.add(checkPointButton);
        linePanel.add(ClickPointButton);
        linePanel.add(deleteTempJsonButton);
        linePanel.add(pauseBtn);
        scriptPanel.add(jb);
        cornPanel.add(runnableButton);
        cornPanel.add(sleepBt);
        cornPanel.add(cornZt);
        runnablePanel.add(rnjb);
        toolsPanel.add(commonModules.getXYBt());
        toolsPanel.add(commonModules.getMoveToXYBt());
        autoScroll.add(autoScrollbtn);
        autoScroll.add(zf2);
        autoScroll.add(zf3);

        this.add(linePanel);
        this.add(StartTempClickButton);
        this.add(StartClickButton);
        this.add(ifLoginButton);
        this.add(ifLoginLable);
        this.add(toolsPanel);
        this.add(scriptPanel);
        this.add(runnablePanel);
        this.add(cornPanel);
        this.add(autoScroll);
        this.add(autoWhellUpbtn);
        this.add(autoWhellDownbtn);
        this.add(zf4);
        this.add(zf5);
        this.add(zf6);
        this.add(zf7);
//        this.add(autoWhell);
        this.setLayout(null);
        this.setOpaque(false);
        this.setVisible(false);
        this.setBounds(0, 0, 335, 518);
    }
    private ZTextField getCornTextField(){
        RunningChain rc = ChainUtil.getRunningChain();
        String cornStr = rc.getCornString();
        ZTextField ztf = new ZTextField(new ZtextDoForRunningChainCorn(),cornStr);
        return ztf;
    }
    private JComboBox getScriptComboBox(){
        JComboBox jb = new JComboBox();
        addItemsFromFile(jb);
        jb.setUI(new MyComboBoxUI());
        jb.setRenderer(new MyListCellRenderer());
        jb.setEnabled(true);
        jb.addPopupMenuListener(new PopupMenuListener() {
            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                jb.removeAllItems();
                addItemsFromFile(jb);
            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                RunningChain rc = ChainUtil.getRunningChain();
                rc.setAutoChainName(jb.getSelectedItem().toString());
                ChainUtil.saveRunningChain(rc);
            }

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {
            }
        });
        return jb;
    }
    private JComboBox getRunnableComboBox(){
        JComboBox jb = new JComboBox();
        addItemsFromSpring(jb);
        jb.setUI(new MyComboBoxUI());
        jb.setRenderer(new MyListCellRenderer());
        jb.setEnabled(true);
        jb.addPopupMenuListener(new PopupMenuListener() {
            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                int index = jb.getSelectedIndex();
                jb.removeAllItems();
                addItemsFromSpring(jb);
                jb.setSelectedIndex(index);
            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                RunningChain rc = ChainUtil.getRunningChain();
                rc.setRunableModuleName(jb.getSelectedItem().toString());
                ChainUtil.saveRunningChain(rc);
            }

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {
            }

        });
        return jb;
    }
    private void addItemsFromFile(JComboBox jb){
        RunningChain rc = ChainUtil.getRunningChain();
        String autoChainName = rc.getAutoChainName();
        String filepath = NativeCp.getUserDir() + "/config/"+ Constant.CLICK_EX;
        File file = new File(filepath);
        int index = 0;
        if (file.exists() ){
            File[] files =  file.listFiles();
            int size = files.length;
            File tempFile;
            int j = 0;
            for (int i = 0; i < size; i++) {
                tempFile = files[i];
                if (tempFile.isDirectory()){
                    jb.addItem(tempFile.getName());
                    if (autoChainName!=null && autoChainName.equals(tempFile.getName())){
                        index = j;
                    }
                    j++;
                }
            }
        }else{
            file.mkdirs();
        }
        jb.setSelectedIndex(index);
    }
    private void addItemsFromSpring(JComboBox jb){
        RunningChain rc = ChainUtil.getRunningChain();
        String runableModuleName = rc.getRunableModuleName();
        Map<String,Runnable> beans = MySpringUtil.getApplicationContext().getBeansOfType(Runnable.class);
        String key;
        Runnable runnable;
        int i = 0;
        int index = 0;
        for (Map.Entry<String,Runnable> entry:beans.entrySet()){
            runnable = entry.getValue();
            key = entry.getKey();
            jb.addItem(key);
            if (runableModuleName!=null && runableModuleName.equals(key)){
                index = i;
            }
            i++;
        }
        jb.setSelectedIndex(index);
    }
    public void setClickManage(ClickManage clickManage) {
        this.clickManage = clickManage;
    }
    private void setLoginByView(BaseZButton ifLoginButton){
        RunningChain rc = ChainUtil.getRunningChain();
        boolean ifLogin = ifLoginButton.getText().contains("√");
        rc.setLogin(ifLogin);
        ChainUtil.saveRunningChain(rc);
    }
}
